package com.xingshentech.ugv.operate.module.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xingshentech.ugv.operate.R;
import com.xingshentech.ugv.operate.module.bean2.CurrentStationInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CurrentStationNumAdapter extends RecyclerView.Adapter<CurrentStationNumAdapter.MyViewHolder> {

    private Context mContext;
    private int pos;
    private List<CurrentStationInfo> mBeanList;
    OnItemClick mOnItemClick;

    public interface OnItemClick {
        void onItemClick(int position, CurrentStationInfo data);
    }

    public CurrentStationNumAdapter(Context context, List<CurrentStationInfo> beanList) {
        mContext = context;
        this.mBeanList = beanList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_current_station_num, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        CurrentStationInfo resultBean = mBeanList.get(position);
        holder.tvListNum.setText(resultBean.getSiteName());
        String id = resultBean.getId();    //站点id

        if (mBeanList.get(position).isSelect()) {
            holder.tvListNum.setSelected(true);
            holder.llItem.setSelected(true);
        } else {
            holder.tvListNum.setSelected(false);
            holder.llItem.setSelected(false);
        }
        holder.llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClick != null) {
                    mOnItemClick.onItemClick(position, resultBean);
                }
                for (int i = 0; i < mBeanList.size(); i++) {
                    if (mBeanList.get(i).getId().equals(mBeanList.get(position).getId())) {
                        mBeanList.get(i).setSelect(true);
                    } else {
                        mBeanList.get(i).setSelect(false);
                    }
                }

                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mBeanList.size();
    }


    public void setOnItemClickListener(OnItemClick mOnItemClick) {
        this.mOnItemClick = mOnItemClick;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_list_num)
        TextView tvListNum;
        @BindView(R.id.ll_item)
        LinearLayout llItem;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
